package com.shop.filter;

import com.alibaba.fastjson.JSON;
import com.shop.utils.ResultUtils;
import org.apache.shiro.web.filter.authc.UserFilter;
import org.springframework.boot.json.GsonJsonParser;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class ShiroUserFilter extends UserFilter {
    /**
     * 重写登录方法
     * @param servletRequest
     * @param servletResponse
     * @throws IOException
     * @throws IOException
     */
    @Override
    protected void redirectToLogin(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, IOException {
        servletResponse.setContentType("application/json; charset=utf-8");
        ResultUtils result = ResultUtils.error(401, "未登录或者登录已失效");

        servletResponse.getWriter().write(JSON.toJSONString(result));
    }

}
